package com.example.mytest6;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;


public class MyFragment5 extends Fragment {
    private BarChart bar_2;

    public MyFragment5() {
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.myfragment5, container, false);
        initView(view);
        initData();
        return view;
    }

    private void initData() {
        bar_2.setDescription(null);
        bar_2.setScaleEnabled(false);
        bar_2.setExtraOffsets(20, 0, 20, 20);

        //Legend  是get方法得到实例，不是new
        Legend legend = bar_2.getLegend();
        legend.setFormSize(14);
        legend.setTextSize(14);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        legend.setDrawInside(true);
        legend.setOrientation(Legend.LegendOrientation.VERTICAL);


        final List<String> xV = new ArrayList<>();
        xV.add("男性");
        xV.add("女性");


        List<BarEntry> yV1 = new ArrayList<>();
        yV1.add(new BarEntry(0, 18800));
        yV1.add(new BarEntry(1, 18700));

        final List<BarEntry> yV2 = new ArrayList<>();
        yV2.add(new BarEntry(0, 15600));
        yV2.add(new BarEntry(1, 15700));


        //dataSet1 是一个类数据对象包含的数据的值文本。 可以理解成一个条形图
        BarDataSet dataSet1 = new BarDataSet(yV1, "有违法");
        dataSet1.setColor(0xFFEA7208);
        dataSet1.setHighlightEnabled(false);
        dataSet1.setValueTextSize(14);
        dataSet1.setValueTextColor(Color.RED);
        //能够被绘制
        dataSet1.isDrawValuesEnabled();
        dataSet1.setValueFormatter(new MyData5Formatter(yV2));

        BarDataSet dataSet2 = new BarDataSet(yV2, "无违法");
        dataSet2.setColor(0xFF6A9701);

        dataSet2.setHighlightEnabled(false);
        //禁止绘制数据对象包含的数据的值文本。
        dataSet2.setDrawValues(false);


        bar_2.getAxisRight().setEnabled(false);
        YAxis yAxis = bar_2.getAxisLeft();
        yAxis.setLabelCount(8, true);
        yAxis.setAxisMinimum(13000);
        yAxis.setAxisMaximum(20000);
        yAxis.setGranularity(1);
        yAxis.setTextSize(14);

        XAxis xAxis = bar_2.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setTextSize(14);
        xAxis.setGranularity(1);
        xAxis.setTypeface(Typeface.DEFAULT_BOLD);
        xAxis.setValueFormatter(new MyXAxis5Formatter(xV));

        List<IBarDataSet> list = new ArrayList<>();
        list.add(dataSet1);
        list.add(dataSet2);
        BarData data = new BarData(list);
        data.setBarWidth(0.5f);
        bar_2.setData(data);
    }

    class MyData5Formatter extends ValueFormatter {
        List<BarEntry> yV2 = new ArrayList<>();

        public MyData5Formatter(List<BarEntry> yV2) {
            this.yV2 = yV2;
        }

        @Override
        public String getBarLabel(BarEntry barEntry) {
            return new DecimalFormat("0.00%").format((barEntry.getY() - yV2.get((int) barEntry.getX()).getY()) / barEntry.getY());
        }
    }

    class MyXAxis5Formatter extends ValueFormatter {
        List<String> xV = new ArrayList<>();

        public MyXAxis5Formatter(List<String> xV) {
            this.xV = xV;
        }

        @Override
        public String getFormattedValue(float value) {
            return xV.get((int) value);
        }
    }


    private void initView(View view) {
        bar_2 = view.findViewById(R.id.bar_2);
    }
}
